{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-29T13:42:25.989860Z",
     "start_time": "2025-09-29T13:42:25.957914Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "from jedi.inference.gradual.annotation import find_type_from_comment_hint_assign\n",
    "\n",
    "df = pd.read_csv('univ.csv', encoding='gbk')\n",
    "df"
   ],
   "id": "d0a72831d009ae39",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "     名次    学校名称      总分  类型 所在省份 所在城市     办学方向 主管部门\n",
       "0     1    北京大学  100.00  综合   北京  北京市    中国研究型  教育部\n",
       "1     2    清华大学   98.50  理工   北京  北京市    中国研究型  教育部\n",
       "2     3    复旦大学   82.79  综合   上海  上海市    中国研究型  教育部\n",
       "3     4    武汉大学   82.43  综合   湖北  武汉市    中国研究型  教育部\n",
       "4     5    浙江大学   82.38  综合   浙江  杭州市    中国研究型  教育部\n",
       "..  ...     ...     ...  ..  ...  ...      ...  ...\n",
       "95   96  浙江师范大学   63.37  师范   浙江  金华市  区域特色研究型  浙江省\n",
       "96   97    安徽大学   63.34  综合   安徽  合肥市    区域研究型  安徽省\n",
       "97   98  首都医科大学   63.32  医药   北京  北京市  区域特色研究型  北京市\n",
       "98   99    江南大学   63.31  综合   江苏  无锡市  区域特色研究型  教育部\n",
       "99  100    山西大学   63.29  综合   山西  太原市    区域研究型  山西省\n",
       "\n",
       "[100 rows x 8 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>名次</th>\n",
       "      <th>学校名称</th>\n",
       "      <th>总分</th>\n",
       "      <th>类型</th>\n",
       "      <th>所在省份</th>\n",
       "      <th>所在城市</th>\n",
       "      <th>办学方向</th>\n",
       "      <th>主管部门</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>北京大学</td>\n",
       "      <td>100.00</td>\n",
       "      <td>综合</td>\n",
       "      <td>北京</td>\n",
       "      <td>北京市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>清华大学</td>\n",
       "      <td>98.50</td>\n",
       "      <td>理工</td>\n",
       "      <td>北京</td>\n",
       "      <td>北京市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>复旦大学</td>\n",
       "      <td>82.79</td>\n",
       "      <td>综合</td>\n",
       "      <td>上海</td>\n",
       "      <td>上海市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>武汉大学</td>\n",
       "      <td>82.43</td>\n",
       "      <td>综合</td>\n",
       "      <td>湖北</td>\n",
       "      <td>武汉市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>浙江大学</td>\n",
       "      <td>82.38</td>\n",
       "      <td>综合</td>\n",
       "      <td>浙江</td>\n",
       "      <td>杭州市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>96</td>\n",
       "      <td>浙江师范大学</td>\n",
       "      <td>63.37</td>\n",
       "      <td>师范</td>\n",
       "      <td>浙江</td>\n",
       "      <td>金华市</td>\n",
       "      <td>区域特色研究型</td>\n",
       "      <td>浙江省</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>97</td>\n",
       "      <td>安徽大学</td>\n",
       "      <td>63.34</td>\n",
       "      <td>综合</td>\n",
       "      <td>安徽</td>\n",
       "      <td>合肥市</td>\n",
       "      <td>区域研究型</td>\n",
       "      <td>安徽省</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>98</td>\n",
       "      <td>首都医科大学</td>\n",
       "      <td>63.32</td>\n",
       "      <td>医药</td>\n",
       "      <td>北京</td>\n",
       "      <td>北京市</td>\n",
       "      <td>区域特色研究型</td>\n",
       "      <td>北京市</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>99</td>\n",
       "      <td>江南大学</td>\n",
       "      <td>63.31</td>\n",
       "      <td>综合</td>\n",
       "      <td>江苏</td>\n",
       "      <td>无锡市</td>\n",
       "      <td>区域特色研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>100</td>\n",
       "      <td>山西大学</td>\n",
       "      <td>63.29</td>\n",
       "      <td>综合</td>\n",
       "      <td>山西</td>\n",
       "      <td>太原市</td>\n",
       "      <td>区域研究型</td>\n",
       "      <td>山西省</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 8 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "collapsed": true
   },
   "cell_type": "markdown",
   "source": [
    "# 二、保存数据\n",
    "\n",
    "## 2.1 保存为csv或txt\n",
    "\n",
    "语法：\n",
    "\n",
    "``` python\n",
    "df.to_csv(\n",
    "    filepath_or_buffer, # 要保存的文件路径\n",
    "    sep = ',', # 列分隔符\n",
    "    columns, # 需要导出的变量列表\n",
    "    header = True, # 指定导出数据的新变量名，可直接提供list\n",
    "    index = True, # 是否导出索引\n",
    "    mode = 'w' : Python写模式，读写方式：r , r+ , w , w+ , a , a+\n",
    "    encoding = 'utf-8', # 默认导出的文件编码格式\n",
    ")\n",
    "```"
   ],
   "id": "dec34d9694e54979"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-29T13:46:32.483309Z",
     "start_time": "2025-09-29T13:46:32.479206Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 导出为txt\n",
    "df.to_csv('rank.txt', columns=['学校名称', '所在城市'], index=False)"
   ],
   "id": "99abd47f82457fc3",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 2.2 保存为excel\n",
    "\n",
    "语法：\n",
    "\n",
    "``` python\n",
    "df.to_excel(\n",
    "    filepath_or_buffer, # 要读入的文件路径\n",
    "    sheet_name = 'Sheet1', # 要保存的表单名称\n",
    ")\n",
    "```"
   ],
   "id": "fa52afd384c9feb9"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-29T13:50:02.951774Z",
     "start_time": "2025-09-29T13:50:02.927621Z"
    }
   },
   "cell_type": "code",
   "source": "df.to_excel('rank.xlsx', index=False, sheet_name='univ_rank')",
   "id": "74090f23dfadcbd0",
   "outputs": [],
   "execution_count": 9
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 2.3 其他格式\n",
    "\n",
    "DataFrame数据框还可以转换为json,markdown,html等其他格式"
   ],
   "id": "3da6586b9894c73b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-29T13:57:45.901137Z",
     "start_time": "2025-09-29T13:57:45.892396Z"
    }
   },
   "cell_type": "code",
   "source": "df.to_html('rank.html')",
   "id": "1b8b038057f46602",
   "outputs": [],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-29T13:58:32.311929Z",
     "start_time": "2025-09-29T13:58:32.298751Z"
    }
   },
   "cell_type": "code",
   "source": "df.to_markdown('rank.md')",
   "id": "db1cbcd75e45a061",
   "outputs": [],
   "execution_count": 11
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
